package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC659 {
    public long startExecuteTime = System.currentTimeMillis();


    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 我还能怎么说呢？首先，你得有个贪心的思路，然后再敢用这个方法
     * @return:
     */
    class Solution {
        public boolean isPossible(int[] nums) {
            Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
            Map<Integer, Integer> endMap = new HashMap<Integer, Integer>();
            for (int x : nums) {
                int count = countMap.getOrDefault(x, 0) + 1;
                countMap.put(x, count);
            }
            for (int x : nums) {
                int count = countMap.getOrDefault(x, 0);
                if (count > 0) {
                    int prevEndCount = endMap.getOrDefault(x - 1, 0);
                    if (prevEndCount > 0) {
                        countMap.put(x, count - 1);
                        endMap.put(x - 1, prevEndCount - 1);
                        endMap.put(x, endMap.getOrDefault(x, 0) + 1);
                    } else {
                        int count1 = countMap.getOrDefault(x + 1, 0);
                        int count2 = countMap.getOrDefault(x + 2, 0);
                        if (count1 > 0 && count2 > 0) {
                            countMap.put(x, count - 1);
                            countMap.put(x + 1, count1 - 1);
                            countMap.put(x + 2, count2 - 1);
                            endMap.put(x + 2, endMap.getOrDefault(x + 2, 0) + 1);
                        } else {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    }

    class Solution_Hard_to_Code {
        public boolean isPossible(int[] nums) {
            if (null == nums || nums.length < 3) return false;

            int curLengthCount = 1, curParallelMove = 1, lastNum = nums[0];
            boolean isFirstSuccessive = true;
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] == lastNum + 1) {
                    ++curLengthCount;
                    lastNum = nums[i];
                } else if (nums[i] == lastNum && !isFirstSuccessive) {
                    isFirstSuccessive = !isFirstSuccessive;


                } else {
                    //不相等产生了中断，需要重新计数
                    if (curLengthCount < 3) return false;
                    curLengthCount = 1;
                    curParallelMove = 1;
                    lastNum = nums[i];
                }
            }

            return true;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,2,3,3,4,5]");
        System.out.println(solution.isPossible(arr));
    }

    public static void main(String[] args) throws Exception {
        LC659 an = new LC659();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
